<?php

namespace App\Containers\AppSection\Accreditation\Tasks;

use App\Containers\AppSection\Accreditation\Data\Repositories\AccreditationRepository;
use App\Containers\AppSection\Data\Data\Repositories\CompanyRepository;
use App\Containers\AppSection\Data\Data\Repositories\PassportRepository;
use App\Containers\AppSection\Data\Data\Repositories\PersonalRepository;
use App\Containers\AppSection\Profile\Data\Repositories\ProfileRepository;
use App\Containers\AppSection\Profile\Models\Profile;
use App\Ship\Exceptions\BadRequestException;
use App\Ship\Parents\Tasks\Task;
use Exception;

class CheckAccreditationTask extends Task
{

    protected AccreditationRepository $repository;
    protected ProfileRepository $profileRepository;
    protected PersonalRepository $personalRepository;
    protected PassportRepository $passportRepository;
    protected CompanyRepository $companyRepository;

    public function __construct(
        AccreditationRepository $repository,
        ProfileRepository       $profileRepository,
        PersonalRepository      $personalRepository,
        PassportRepository      $passportRepository,
        CompanyRepository       $companyRepository
    )
    {
        $this->companyRepository = $companyRepository;
        $this->passportRepository = $passportRepository;
        $this->personalRepository = $personalRepository;
        $this->profileRepository = $profileRepository;
        $this->repository = $repository;
    }

    public function run($profileId)
    {
        try {
            /** @var Profile $profile */
            $profile = $this->profileRepository->find($profileId);

            if ($this->personalRepository->check($profile->legal_form->personal->id, $profile->legal_form_type, $profile->user->roles[0]->name))
                throw new Exception('Не заполнены персональные данные!');

            if ($this->passportRepository->check($profile->legal_form->passport->id))
                throw new Exception('Не заполнены паспортные данные!');

            if ($profile->hasLegalForm('entity') && $this->companyRepository->check($profile->legal_form->company->id))
                throw new Exception('Не заполнены данные компании!');

        } catch (Exception $exception) {
            throw new BadRequestException($exception->getMessage());
        }
    }
}
